<?php

namespace App\Service\Admin;

use App\Model\Admin\AttendClassModel;
use App\Model\Admin\AttendPeriodModel;
use App\Service\BaseService;

/**
 * 考勤班次管理-服务类
 * @author LZH
 * @since: 2021/08/12
 * Class AttendClassService
 */
class AttendClassService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new AttendClassModel();
        parent::__construct();
    }

    /**
     * 获取数据列表
     * @return array
     * @since 2021/08/12
     * @author LZH
     */
    public function getList()
    {
        $param = $this->getRequestParam();

        // 查询条件
        $map = [];

        // 需要连接查询attend_period表
        $result = $this->model->join('sc_attend_period', 'sc_attend_period.class_id = sc_attend_class.id')
            ->field(['sc_attend_class.*', "group_concat(concat_ws('-',sc_attend_period.start_time, sc_attend_period.end_time) SEPARATOR ';') as attend_period"])
            ->order('period_name', 'asc')
            ->group('id')
            ->all()
            ->toArray(true, false);

//        return [
//            "data" => $result,
//            "count" => count($result),
//        ];
        return $result;
    }

    // TODO 这里是使用关联查询来获取信息，不能使用常规的info方法
    public function info()
    {
        return parent::info();
    }


    public function edit()
    {
        $classId = parent::edit();

        /*
        Array
            (
                [0] => Array
                    (
                        [goToSchoolTime] => 15:22
                        [signInTime] => Array
                            (
                                [0] => 15:22
                                [1] => 16:22
                            )

                        [leaveSchoolTime] => 15:22
                        [signOutTime] => Array
                            (
                                [0] => 15:22
                                [1] => 16:22
                            )

                    )

                [1] => Array
                    (
                        [goToSchoolTime] => 15:22
                        [signInTime] => Array
                            (
                                [0] => 15:22
                                [1] => 16:22
                            )

                        [leaveSchoolTime] => 15:22
                        [signOutTime] => Array
                            (
                                [0] => 15:22
                                [1] => 16:22
                            )

                    )

                [2] => Array
                    (
                        [goToSchoolTime] => 15:22
                        [signInTime] => Array
                            (
                                [0] => 15:22
                                [1] => 16:22
                            )

                        [leaveSchoolTime] => 15:22
                        [signOutTime] => Array
                            (
                                [0] => 15:22
                                [1] => 16:22
                            )

                    )

            )
        */
        $classPeriod = $this->getRequestParam('classPeriod');

        $classPeriod = array_filter($classPeriod); // 过滤空成员
        $attendPeriods = [];
        foreach ($classPeriod as $index => $period) {
            $attendPeriods[] = [
                'class_id' => $classId,
                'start_time' => $period['goToSchoolTime'],
                'end_time' => $period['leaveSchoolTime'],
                'start_check_in_time' => $period['signInTime'][0],
                'end_check_in_time' => $period['signInTime'][1],
                'start_check_out_time' => $period['signOutTime'][0],
                'end_check_out_time' => $period['signOutTime'][1],
                'period_name' => $index + 1, // 班段
                'period_type' => $index + 1, // 班段类型：1第一时段上下学 2第二时段上下学 3第三时段上下学
                // TODO 通用字段需要在架构层次解决
            ];
        }

        $attendPeriodModel = new AttendPeriodModel();
        $result = $attendPeriodModel->saveAll($attendPeriods);
        return array_values($result);
    }
}
